package com.thon.controller.teacher;

import com.thon.controller.util.BaseController;
import com.thon.entity.topic.Topic;
import com.thon.service.system.SystemService;
import com.thon.service.topic.TopicService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by Chen on 2016/3/11.
 */
@Controller
@RequestMapping(value = "/teacher")
public class TeacherController extends BaseController{

    @Autowired
    private SystemService systemService;
    @Autowired
    private TopicService topicService;

    @RequestMapping(method = RequestMethod.GET)
    public String student() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "teacher";
    }

    @RequestMapping(value="/user/profile", method = RequestMethod.GET)
    public String teacherProfile(Model model) {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        int userId = getCurrentUserId();
        model.addAttribute("user", systemService.getUser(userId));
        return "teacher-user-profile";
    }

    @RequestMapping(value="/topic/manage", method = RequestMethod.GET)
    public String topicManage() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "teacher-topic-manage";
    }

    @RequestMapping(value="/topic/form", method = RequestMethod.GET)
    public String topicLookForm(@RequestParam(value="id",required=true) Integer id, Model model) {

        Topic topic = topicService.getTopic(id);

        model.addAttribute("topic", topic);
        return "teacher-topic-form";
    }

    @RequestMapping(value="/topic/mine", method = RequestMethod.GET)
    public String topicMine() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "teacher-topic-mine";
    }

    @RequestMapping(value="/topic/add/form", method = RequestMethod.GET)
    public String topicAddForm(@RequestParam(value="id",required=false) Integer id, Model model) {

        Topic topic = new Topic();
        if (id != null) {
            topic = topicService.getTopic(id);
        }
        model.addAttribute("topic", topic);
        return "teacher-topic-add-form";
    }

    @RequestMapping(value="/topic/check/manage", method = RequestMethod.GET)
    public String topicCheck() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "teacher-topic-check-manage";
    }

    @RequestMapping(value="/choice/manage", method = RequestMethod.GET)
    public String choiceManage() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "teacher-choice-manage";
    }

    @RequestMapping(value="/user/password", method = RequestMethod.GET)
    public String editPassword() {
        if (!SecurityUtils.getSubject().isAuthenticated()){
            return "login";
        }
        return "teacher-user-password";
    }

}
